﻿using Autofac;
using FluentScheduler;
using Pay.Core;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using Pay.IService;
using Flurl.Http;
using Pay.DTO;
using System.Threading.Tasks;

namespace Pay.Web.Utils {
    /// <summary>
    /// 系统定时计划任务
    /// </summary>
    public class TokenScheduler : Registry {
#if DEBUG
        private const string api_url = "http://192.168.10.254:3001/{0}";
#else
        private const string api_url = "http://api.larabo.cn/{0}";
#endif
        public TokenScheduler() {
            #region 处理同步产品数据
            //同步商品数据
            Schedule(() => {
                var log = IocHelper.Get<ILogHelper>("info");
                try {
                    log.Info("处理HtlJob定时任务开始：" + DateTime.Now);
                    HtlJob();
                } catch (Exception ex) {
                    log.Info("处理HtlJob定时任务异常：" + ex.Message);
                }
            }).ToRunEvery(10).Minutes();//每10分钟执行
            //查询订单状态同步数据库
            Schedule(() => {
                var log = IocHelper.Get<ILogHelper>("info");
                try {
                    log.Info("同步海淘乐订单状态据定时任务开始：" + DateTime.Now);
                    GetOrdersStatus();
                } catch (Exception ex) {
                    log.Info("同步海淘乐订单状态定时任务异常：" + ex.Message);
                }
            }).ToRunEvery(1).Hours();//每10分钟执行
            //推送订单数据
            //Schedule(()=> {
            //    var log = IocHelper.Get<ILogHelper>("info");
            //    try {
            //        log.Info("推送海淘乐订单状态据定时任务开始：" + DateTime.Now);
            //        //PushOrder();
            //    } catch (Exception ex) {
            //        log.Info("推送海淘乐订单状态定时任务异常：" + ex.Message);
            //    }
            //}).ToRunEvery(1).Hours();//每1小时执行

            //Schedule(() => {
            //    var log = IocHelper.Get<ILogHelper>("info");
            //    try {
            //        log.Info("token排行榜定时任务开始：" + DateTime.Now);
            //        RedRank();
            //    } catch (Exception ex) {
            //        log.Info("token排行榜定时任务异常：" + ex.Message);
            //    }
            //}).ToRunEvery(50).Minutes();//每50分钟执行

            Schedule(() =>
            {
                var log = IocHelper.Get<ILogHelper>("info");
                try {
                    log.Info("睿享金服 数据处理定时任务开始：" + DateTime.Now);
                    RXJFHandle();
                }
                catch (Exception ex) {
                    log.Info("睿享金服 数据处理定时任务异常：" + ex.Message);
                }
            }).ToRunEvery(1).Days().At(04, 30);//暂定 每天早上4点半
            //拓垦集市定时收货
            Schedule(() => {
                var log = IocHelper.Get<ILogHelper>("info");
                try {
                    log.Info("拓垦集市定时收货：" + DateTime.Now);
                    BazaarOrderRecive();
                } catch (Exception ex) {
                    log.Info("拓垦集市定时收货定时任务异常：" + ex.Message);
                }
            })
#if DEBUG
            .ToRunEvery(5).Minutes();
#else
            .ToRunEvery(2).Hours() ;
#endif

            //拓垦统计并更新总马力
            Schedule(() =>
            {
                var log = IocHelper.Get<ILogHelper>("info");
                try {
                    log.Info("拓垦统计并更新总马力 数据处理定时任务开始：" + DateTime.Now);
                    StatisticsTokenTotalHorsePower();
                }
                catch (Exception ex) {
                    log.Info("拓垦统计并更新总马力 数据处理定时任务异常：" + ex.Message);
                }
            }).ToRunEvery(1).Days().At(01, 00);//暂定 每天早上1点
            #endregion
        }

        private void HtlJob() {
            var log = IocHelper.Get<ILogHelper>("info");
            try {
                var result = string.Format(api_url, "htl_job").PostJsonAsync(new head_req { }).ReceiveString().Result;
            } catch (Exception ex) {
                log.Info("请求异常：" + ex.Message);
            }
        }

        private void GetOrdersStatus() {
            var log = IocHelper.Get<ILogHelper>("info");
            try {
                var result = string.Format(api_url, "htl_orders_status").PostJsonAsync(new head_req { }).ReceiveString().Result;
            } catch (Exception ex) {
                log.Info("请求异常：" + ex.Message);
            }
        }

        private void PushOrder() {
            var log = IocHelper.Get<ILogHelper>("info");
            try {
                var result = string.Format(api_url, "htl_push_order").PostJsonAsync(new head_req { }).ReceiveString().Result;
            } catch (Exception ex) {
                log.Info("请求异常：" + ex.Message);
            }
        }

        private void RedRank() {
            var log = IocHelper.Get<ILogHelper>("info");
            try {
                var result = string.Format(t_RequestMethod.token_url + "{0}", "Red/task_red_rank").GetAsync().ReceiveString().Result;
            } catch (Exception ex) {
                log.Info("请求异常：" + ex.Message);
            }
        }
        /// <summary>
        /// 集市定时收货
        /// </summary>
        private void BazaarOrderRecive() {
            var log = IocHelper.Get<ILogHelper>("info");
            try {
                var result = string.Format(t_RequestMethod.token_url + "{0}", "bazzar_product/task_bazaar_order_recive").GetAsync().ReceiveString().Result;
            } catch (Exception ex) {
                log.Info("请求异常：" + ex.Message);
            }
        }

        /// <summary>
        /// 睿享金服 数据处理
        /// </summary>
        private void RXJFHandle() {
            var log = IocHelper.Get<ILogHelper>("info");
            try {
#if !DEBUG
                var result = string.Format(t_RequestMethod.token_url + "{0}", "rxjf/RXJFDataHandle").GetAsync().ReceiveString().Result;
#endif
            }
            catch (Exception ex) {
                log.Info("请求异常：" + ex.Message);
            }
        }

        /// <summary>
        /// 统计并更新拓垦总马力设置
        /// </summary>
        private void StatisticsTokenTotalHorsePower() {
            var log = IocHelper.Get<ILogHelper>("info");
            try {
                var result = string.Format(t_RequestMethod.token_url + "{0}", "Red/StatisticsTokenTotalHorsePower").GetAsync().ReceiveString().Result;
            }
            catch (Exception ex) {
                log.Info("请求异常：" + ex.Message);
            }
        }

    }
    /// <summary>
    /// 候鸟定时计划任务
    /// </summary>
    public class HniaoScheduler : Registry {
#if DEBUG
        private const string api_url = "http://192.168.10.254:3001/{0}";
#else
        private const string api_url = "http://api.larabo.cn/{0}";
#endif
        public HniaoScheduler() {
            //同步商品数据
            Schedule(() => {
                var log = IocHelper.Get<ILogHelper>("info");
                try {
                    log.Info("处理同步产品数据定时任务开始：" + DateTime.Now);
                    GetProducts();
                } catch (Exception ex) {
                    log.Info("处理同步产品数据定时任务异常：" + ex.Message);
                }
            }).ToRunEvery(1).Days().At(15, 30);//每天下午3点30分钟执行

            //查询订单状态同步数据库
            Schedule(() => {
                var log = IocHelper.Get<ILogHelper>("info");
                try {
                    log.Info("同步候鸟订单状态据定时任务开始：" + DateTime.Now);
                    GetOrdersStatus();
                } catch (Exception ex) {
                    log.Info("同步候鸟订单状态定时任务异常：" + ex.Message);
                }
            }).ToRunEvery(1).Hours();//每10分钟执行
            //推送订单数据
            Schedule(() => {
                var log = IocHelper.Get<ILogHelper>("info");
                try {
                    log.Info("推送候鸟订单状态据定时任务开始：" + DateTime.Now);
                    //PushOrder();
                } catch (Exception ex) {
                    log.Info("推送候鸟订单状态定时任务异常：" + ex.Message);
                }
            }).ToRunEvery(1).Days().At(15, 30);//每天下午3点30分钟执行
        }

        private void GetOrdersStatus() {
            var log = IocHelper.Get<ILogHelper>("info");
            try {
                var result = string.Format(api_url, "hniao_order_status").PostJsonAsync(new head_req { }).ReceiveString().Result;
            } catch (Exception ex) {
                log.Info("请求异常：" + ex.Message);
            }
        }

        private void PushOrder() {
            var log = IocHelper.Get<ILogHelper>("info");
            try {
                var result = string.Format(api_url, "hniao_push_order").PostJsonAsync(new head_req { }).ReceiveString().Result;
            } catch (Exception ex) {
                log.Info("请求异常：" + ex.Message);
            }
        }

        private void GetProducts() {
            var log = IocHelper.Get<ILogHelper>("info");
            try {
                var result = string.Format(api_url, "hniao_get_product").GetAsync().ReceiveString().Result;
            } catch (Exception ex) {
                log.Info("请求异常：" + ex.Message);
            }
        }
    }
    /// <summary>
    /// 自动收货、自动结算定时任务
    /// </summary>
    public class BillScheduler : Registry {
#if DEBUG
        private const string api_url = "http://192.168.10.254:3001/{0}";
#else
        private const string api_url = "http://api.larabo.cn/{0}";
#endif

        public BillScheduler() {
            Schedule(() => {
                var log = IocHelper.Get<ILogHelper>("info");
                try {
                    log.Info("同步自动收货定时任务开始：" + DateTime.Now);
                    AutoOrderFinish();
                } catch (Exception ex) {
                    log.Info("同步自动收货定时任务异常：" + ex.Message);
                }
            }).ToRunEvery(1).Days().At(10, 30);//每天10点半执行


            Schedule(() => {
                var log = IocHelper.Get<ILogHelper>("info");
                try {
                    log.Info("同步自动结算定时任务开始：" + DateTime.Now);
                    AutoSettleAccount();
                } catch (Exception ex) {
                    log.Info("同步自动结算定时任务异常：" + ex.Message);
                }
            })
#if DEBUG
            .ToRunEvery(2).Minutes();//每1小时执行
#else
            .ToRunEvery(1).Hours();//每1小时执行
#endif
        }


        private void AutoOrderFinish() {
            var log = IocHelper.Get<ILogHelper>("info");
            try {
                var result = string.Format(api_url, "auto_order_finish").PostJsonAsync(new head_req { }).ReceiveString().Result;
            } catch (Exception ex) {
                log.Info("请求异常：" + ex.Message);
            }
        }

        private void AutoSettleAccount() {
            var log = IocHelper.Get<ILogHelper>("info");
            try {
                var result = string.Format(api_url, "auto_settle_accounts").PostJsonAsync(new head_req { }).ReceiveString().Result;
            } catch (Exception ex) {
                log.Info("请求异常：" + ex.Message);
            }
        }
    }
}
